<?php

namespace App\Http\Controllers\Admin\V1;

use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\CategoryGroup;
use App\Services\CategoryGroup\CategoryGroupInterface;
use Dingo\Api\Http\Response;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CategoryController extends Controller
{
    /**
     * 分类列表
     *
     * @param Request $request
     * @return array
     */
    public function index(Request $request)
    {
        // 返回缓存分类数据
        return categoryGroup($request->all());
    }

    /**
     * 创建分类
     *
     * @param Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'category_group' => ['required', 'string', Rule::in(Category::CATEGORY_GROUP)]
        ]);

        // 获取分组
        $categoryGroup = $request->input('category_group');

        // 获取类名
        $namespace = '\\App\\Services\\CategoryGroup\\Group\\' . camelCase($categoryGroup);
        // 判断类名是否存在
        if (!class_exists($namespace)) {
            return $this->response->errorNotFound($namespace . '未定义');
        }

        // 解析类
        $class = resolve($namespace);

        // 判断类是否实现CategoryGroupInterface的接口
        if (!$class instanceof CategoryGroupInterface) {
            return $this->response->errorNotFound('CategoryGroupInterface未实现');
        }

        // 获取分类名称
        $name = $request->input('name');

        // 删除用户输入的 flag
        $request->offsetUnset('flag');

        // 自己转拼音
        $flag = pinyin($name, PINYIN_KEEP_ENGLISH);

        $categoryGroupId = CategoryGroup::where('flag', $request->input('category_group'))->first();

        $request->offsetSet('category_group_id', $categoryGroupId->id);

        // 增加字段
        $request->offsetSet('flag', implode('', $flag));

        $request->offsetSet('user_id', $this->adminUser()->id());

        $class->store($request);

        // 删除缓存
        destroyCategoryCache($categoryGroup);

        return $this->response->created();
    }

    /**
     * 修改分类
     *
     * @param Request $request
     * @param Category $category
     * @return Response|void
     */
    public function update(Request $request, Category $category)
    {
        $request->validate([
            'category_group' => ['required', 'string', Rule::in(Category::CATEGORY_GROUP)],
        ]);

        // 获取分组
        $group = $request->input('category_group');

        // 获取类名
        $namespace = '\\App\\Services\\CategoryGroup\\Group\\' . ucfirst($group);

        // 判断类名是否存在
        if (!class_exists($namespace)) {
            return $this->response->errorNotFound($namespace . '未定义');
        }

        // 解析类
        $class = resolve($namespace);

        // 判断类是否实现CategoryGroupInterface的接口
        if (!$class instanceof CategoryGroupInterface) {
            return $this->response->errorNotFound('CategoryGroupInterface未实现');
        }

        // 获取分类名称
        $name = $request->input('name') ?? $category->name;

        // 删除用户输入的 flag
        $request->offsetUnset('flag');

        // 自己转拼音
        $flag = pinyin($name, PINYIN_KEEP_ENGLISH);

        // 增加字段
        $request->offsetSet('flag', implode('', $flag));

        $class->update($request, $category);

        // 删除缓存
        destroyCategoryCache($group);

        return $this->response->noContent();
    }

    /**
     * 禁用启用分类
     *
     * @param Category $category
     * @return Response|void
     */
    public function destroy(Category $category)
    {
        $category->status = !$category->status;
        $category->save();

        // 删除缓存
        destroyCategoryCache(camelCase($category->categoryGroup->flag));

        return $this->response->noContent();
    }
}
